﻿using MySql.Data.EntityFramework;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;

namespace OrderControl {
    [DbConfigurationType(typeof(MySqlEFConfiguration))]
    public class OrderModel : DbContext {
        //您的上下文已配置为从您的应用程序的配置文件(App.config 或 Web.config)
        //使用“OrderModel”连接字符串。默认情况下，此连接字符串针对您的 LocalDb 实例上的
        //“OrderControl.OrderModel”数据库。
        // 
        //如果您想要针对其他数据库和/或数据库提供程序，请在应用程序配置文件中修改“OrderModel”
        //连接字符串。
        public OrderModel()
            : base("name=OrderModel") {
        }

        //为您要在模型中包含的每种实体类型都添加 DbSet。有关配置和使用 Code First  模型
        //的详细信息，请参阅 http://go.microsoft.com/fwlink/?LinkId=390109。

        public virtual DbSet<Commodity> Commodities { get; set; }
        public virtual DbSet<Customer> Customers { get; set; }
        public virtual DbSet<OrderDetail> OrderDetails { get; set; }
        public virtual DbSet<Order> Orders { get; set; }
    }

    public class Commodity {
        private int commodityId;
        private string name;
        private double unitPrice;

        public int CommodityId { get => commodityId; set => commodityId = value; }
        public string Name { get => name; set => name = value; }
        public double UnitPrice { get => unitPrice; set => unitPrice = value; }

        public Commodity() { }
        public Commodity(string name,double unitPrice) {
            this.Name = name;
            this.UnitPrice = unitPrice;
        }
    }

    public class Customer {
        private int customerId;
        private string name;
        private string info;

        public int CustomerId { get => customerId; set => customerId = value; }
        public string Name { get => name; set => name = value; }
        public string Info { get => info; set => info = value; }

        public Customer() {
            Name = "Null";
            Info = "Null";
        }
        public Customer(string name) {
            Name = name;
            Info = "Null";
        }
        public Customer(string name, string info) {
            Name = name;
            Info = info;
        }
    }

    public class OrderDetail {
        private int orderDetailId;
        private Commodity commodity;
        private int amount = 0;

        public int OrderDetailId { get => orderDetailId; set => orderDetailId = value; }
        public int Amount { get => amount; set => amount = value; }
        public Commodity Commodity { get => commodity; set => commodity = value; }
        public double Price { get => commodity.UnitPrice * amount; }
        [NotMapped]
        [System.Xml.Serialization.XmlIgnore]
        public string CommodityName { get => commodity.Name; set => commodity.Name = value; }
        [NotMapped]
        [System.Xml.Serialization.XmlIgnore]
        public double CommodityUnitPrice { get => commodity.UnitPrice; set => commodity.UnitPrice = value; }

        public OrderDetail() { }
        public OrderDetail(Commodity commodity,int amount) {
            this.Commodity = commodity;
            this.Amount = amount;
        }
    }

    public class Order {
        private int orderId;
        private List<OrderDetail> details = new List<OrderDetail>();
        private Customer customer;
        private DateTime time;
        private string shippingAddress;

        public int OrderId { get => orderId; set => orderId = value; }
        public DateTime Time { get => time; set => time = value; }
        public string ShippingAddress { get => shippingAddress; set => shippingAddress = value; }
        public List<OrderDetail> Details { get => details; set => details = value; }
        public Customer Customer { get => customer; set => customer = value; }
        [NotMapped]
        [System.Xml.Serialization.XmlIgnore]
        public String CustomerName { get => customer.Name; set => customer.Name = value; }
        [NotMapped]
        [System.Xml.Serialization.XmlIgnore]
        public String CustomerInfo { get => customer.Info; set => customer.Info = value; }
        public double TotalPrice {
            get {
                double totalPrice = 0;
                foreach (OrderDetail orderDetail in Details) {
                    totalPrice += orderDetail.Price;
                }
                return totalPrice;
            }
        }

        public Order() { }

        public Order(Customer customer, DateTime time, String shippingAddress) {
            this.Customer = customer;
            this.Time = time;
            this.ShippingAddress = shippingAddress;
        }

        public override string ToString() {
            return "[Order] Id: " + OrderId
                + ", Customer: " + Customer.Name
                + ", Time: " + Time
                + ", Total Price: " + TotalPrice
                + ", Shipping address: " + ShippingAddress;
        }
    }
}